import api from "../../api/index";
// pages/ranking/ranking.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
        closeCustom: true,
      },
    ],
    sExplain: {
      title: "排行榜说明",
      content: "可以根据时间，统计当前门店及当前区域门店排名及相关数据，可按销售总额、单数、上课金额、节数、授课金额、课数、售卡金额、卡数进行排序",
    },
    searchParam: {
      time_form: "today",
    },
    order: [],
    sort_field: "",
    //#endregion
    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },
  getDataList() {
    let { searchParam, sort_field } = this.data;
    let data = {
      ...searchParam
    }
    if(sort_field) data.sort_field = sort_field
    this.setData({
      isLoading: true,
    })
    api
      .apiStoreRank(data)
      .then((res) => {
        let data = res.data;
        this.setData({
          list: data.shops_rank,
          order: data.sort_field,
          userInfo: data.my_rank,
          shopInfo: data.shop_info
        });
        if(!sort_field){
          this.setData({
            sort_field: data.sort_field[0].value
          })
        }
      }).finally(_ => {
        this.setData({
          isLoading: false,
        })
      });
  },
  switchOrder(e){
    this.setData({
      sort_field: e.currentTarget.dataset.value,
      list: []
    })
    this.getDataList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
